package cn.admobiletop.adsuyi.adapter.toutiao.listener;

import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTSplashAd;

import cn.admobiletop.adsuyi.ad.listener.ADSuyiSplashAdListener;
import cn.admobiletop.adsuyi.ad.widget.ADSuyiSplashAdContainer;
import cn.admobiletop.adsuyi.adapter.toutiao.data.SplashAdInfo;
import cn.admobiletop.adsuyi.config.ADSuyiErrorConfig;
import cn.admobiletop.adsuyi.util.ADSuyiViewUtil;

/**
 * @author ciba
 * @description 开屏广告监听
 * @date 2020/4/2
 */
public class SplashAdListener extends BaseAdListener<ADSuyiSplashAdListener> implements TTAdNative.SplashAdListener {
    private ADSuyiSplashAdContainer adContainer;
    private SplashAdInfo splashAdInfo;
    private Handler handler = new Handler(Looper.getMainLooper());

    public SplashAdListener(ADSuyiSplashAdContainer adContainer, String platformPosId, ADSuyiSplashAdListener adListener) {
        super(platformPosId, adListener);
        this.adContainer = adContainer;
    }

    @Override
    public void onError(final int code, final String message) {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    onAdFailed(code, message);
                }
            });
        }
    }

    @Override
    public void onTimeout() {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    onAdFailed(ADSuyiErrorConfig.AD_FAILED_TIME_OUT, ADSuyiErrorConfig.MSG_AD_FAILED_TIME_OUT);
                }
            });
        }
    }

    @Override
    public void onSplashAdLoad(final TTSplashAd ttSplashAd) {
        // 广告对象为空，返回错误回调
        if (ttSplashAd == null) {
            if (handler != null) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onAdFailed(ADSuyiErrorConfig.AD_FAILED_AD_IS_EMPTY, ADSuyiErrorConfig.MSG_AD_FAILED_AD_IS_EMPTY);
                    }
                });
            }
            return;
        }

        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null && adContainer != null) {
                        splashAdInfo = new SplashAdInfo(getPlatformPosId());
                        splashAdInfo.setAdapterAdInfo(ttSplashAd);
                        splashAdInfo.setAdListener(getAdListener());
                        addSplashAdView(ttSplashAd.getSplashView());
                        getAdListener().onAdReceive(splashAdInfo);
                    }
                }
            });
        }
    }

    /**
     * 添加开屏广告视图
     *
     * @param splashAdView       开屏广告视图
     */
    private void addSplashAdView(View splashAdView) {
        if (splashAdView != null && adContainer != null) {
            ADSuyiViewUtil.removeSelfFromParent(splashAdView);
            adContainer.addView(splashAdView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }
    }

    /**
     * 释放广告资源
     */
    @Override
    public void release() {
        super.release();
        if (splashAdInfo != null) {
            splashAdInfo.release();
            splashAdInfo = null;
        }
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }
}
